package cn.maidouya.product.controller;

import cn.maidouya.basic.exception.BusinessExcepiton;
import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.basic.util.PageList;
import cn.maidouya.product.domain.Product;
import cn.maidouya.product.domain.ProductDetail;
import cn.maidouya.product.query.ProductQuery;
import cn.maidouya.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/product")
@Api(tags = "服务接口",description = "服务接口详细描述")
public class ProductController {
    @Autowired
    private IProductService productService;

    /**
     *  根据服务id查询详情页信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据服务id查询详情页信息",notes = "根据服务id查询详情页信息")
    public Product loadById(@PathVariable Long id) {
        Product one = productService.loadById(id);
        return one;
    }

    /**
     *   服务和详情页回显
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "查找服务",notes = "根据ID查找服务")
    public ProductDetail loadDetailByProductId(@PathVariable("id") Long id) {
        System.err.println(id);
        return productService.loadDetailByProductId(id);
    }
    @GetMapping
    @ApiOperation(value = "查找全部服务",notes = "查找所有服务信息")
    public List<Product> loadAll() {
        List<Product> list = productService.loadAll();
        return list;
    }
    @PostMapping
    @ApiOperation(value = "查找服务分页数据",notes = "分页查找所有服务信息")
    public PageList<Product> queryData(@RequestBody ProductQuery productQuery) {
        PageList<Product> pageList = productService.queryData(productQuery);
        return pageList;
    }

    /**
     *   前端首页展示
     * @param productQuery
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "前端首页查找分页和所有展示",notes = "前端首页查找分页和所有展示")
    public PageList<Product> queryList(@RequestBody ProductQuery productQuery) {
        productQuery.setState(1);
        PageList<Product> pageList = productService.queryData(productQuery);
        return pageList;
    }
    @PutMapping
    @ApiOperation(value = "添加和修改服务",notes = "添加和修改服务、详情页")
    public AjaxResult saveAndUpdate(@RequestBody Product product) {
                try {
                    if (product.getId() == null){
                        productService.save(product);
                    } else {
                        productService.update(product);
                    }
                    return new AjaxResult();
                } catch (Exception e) {
                    e.printStackTrace();
                    return  AjaxResult.getResult().setSuccess(false).setMessage("操作失败");
                }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除服务",notes = "根据id删除服务和详情页")
    public AjaxResult delete(@PathVariable Long id) {
        System.err.println(id);
        try {
          productService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.getResult().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     *   批量上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            productService.onOROffSale(ids,1); // 1上架 0下架
            return AjaxResult.getResult();
        } catch (BusinessExcepiton e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage("系统繁忙，稍后再试！");
        }
    }
    /**
     *   批量下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            productService.onOROffSale(ids,0);// 1上架 0下架
            return AjaxResult.getResult();
        } catch (BusinessExcepiton e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage("系统繁忙，稍后再试！");
        }
    }
    /**
     *   批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        System.err.println("批量删除： " +ids);
        try {
            productService.batchDelete(ids); // 批量删除
            return AjaxResult.getResult();
        } catch (BusinessExcepiton e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage("系统繁忙，稍后再试！");
        }
    }

    @PostMapping("/match")
    @ApiOperation(value = "右侧经典搭配查询",notes = "右侧经典搭配查询")
    public List<Product> loadMatch() {
        return productService.loadMatch();
    }
}